package net.jwtools.tmt.ui;

import java.awt.Color;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import net.jwtools.tmt.service.IVisitService;
import net.jwtools.tmt.service.VisitService;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Week;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class VisitHistoryJPanel extends JPanel {
	private IVisitService visitService;

	public VisitHistoryJPanel(IVisitService service) {
		this.visitService = service;
	}

	public JComponent getControl() {
		JFreeChart chart = createChart(createDataset());
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(269, 241));
		return chartPanel;
	}

	private static JFreeChart createChart(XYDataset xydataset) {
		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
				"History checked out territories", "Date", "Number", xydataset,
				true, true, false);
		//jfreechart.setBackgroundPaint(Color.white);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		// xyplot.setBackgroundPaint(Color.lightGray);
		// xyplot.setDomainGridlinePaint(Color.white);
		// xyplot.setRangeGridlinePaint(Color.white);
		xyplot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
		xyplot.setDomainCrosshairVisible(true);
		xyplot.setRangeCrosshairVisible(true);
		org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot
				.getRenderer();
		if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
			xylineandshaperenderer.setShapesVisible(Boolean.TRUE);
			// xylineandshaperenderer.setBaseLinesVisible(flag)setDefaultShapesVisible(true);
			// xylineandshaperenderer.setDefaultShapesFilled(true);
		}
		DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
		// dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
		return jfreechart;
	}

	private XYDataset createDataset() {
		TimeSeries regularVisitsHistoryByWeek = new TimeSeries(
				"Regular visits", org.jfree.data.time.Week.class);
		Map<Week, Integer> rvh = this.visitService
				.getRegularVisitsHistoryByWeek();

		for (Iterator iterator = rvh.keySet().iterator(); iterator.hasNext();) {
			Week week = (Week) iterator.next();
			regularVisitsHistoryByWeek.add(week, rvh.get(week));
		}
		TimeSeries reVisitsHistoryByWeek = new TimeSeries("Revisits",
				org.jfree.data.time.Week.class);
		Map<Week, Integer> rrvh = this.visitService.getReVisitsHistoryByWeek();

		for (Iterator iterator = rrvh.keySet().iterator(); iterator.hasNext();) {
			Week week = (Week) iterator.next();
			reVisitsHistoryByWeek.add(week, rrvh.get(week));
		}
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		timeseriescollection.addSeries(regularVisitsHistoryByWeek);
		timeseriescollection.addSeries(reVisitsHistoryByWeek);
		// timeseriescollection.setDomainIsPointsInTime(true);
		return timeseriescollection;
	}

	public static void main(String args[]) {
		JFrame frame = new JFrame();
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] {
						"/net/jwtools/tmt/ctx/tmt-application-context.xml",
						"/net/jwtools/tmt/ctx/tmt-data-access-context.xml" });

		BeanFactory factory = (BeanFactory) context;
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		VisitHistoryJPanel spp = new VisitHistoryJPanel((VisitService) factory
				.getBean("visitService"));
		frame.setContentPane(spp.getControl());
		frame.pack();
		frame.setVisible(true);
	}
}
